package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.QuanMaoCardDAO;
import com.ecoolex.discount.sales.core.external.pojo.QuanMaoCardListResult.QuanMaoCardVoucher;
import com.ecoolex.discount.sales.entity.QuanMaoCard;
import com.ecoolex.discount.sales.entity.QuanMaoCard.Column;
import com.ecoolex.discount.sales.entity.QuanMaoCardExample;
import com.ecoolex.discount.sales.enums.QuanMaoCardDataStatus;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;
import java.util.List;

import static com.ecoolex.discount.sales.entity.QuanMaoCard.NOT_DELETED;

@Service
public class QuanMaoCardManager {
    @Autowired
    private QuanMaoCardDAO dao;

    public Pagination<QuanMaoCard> query(Sorting[] sorts, Page page) {
        QuanMaoCardExample example = new QuanMaoCardExample();

        example.orderBy(Sortings.builder(sorts).toSql());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }

    public List<QuanMaoCard> queryAll() {
        QuanMaoCardExample example = new QuanMaoCardExample();
        example.createCriteria()
                .andDeletedEqualTo(NOT_DELETED);
        return dao.selectByExample(example);
    }

    public QuanMaoCard getById(String cardNo) {
        return dao.selectByPrimaryKeyWithLogicalDelete(cardNo, NOT_DELETED);
    }

    public void create(QuanMaoCardVoucher card, String createUser) {
        QuanMaoCard record = new QuanMaoCard();
        record.setCardNo(card.getCardNo());
        record.setCardName(card.getCardName());
        record.setCardType(card.getCardType());
        record.setType(card.getType());
        record.setStatus(card.getStatus());
        record.setFaceValue(BigDecimal.valueOf(card.getFaceValue()));
        record.setPrice(BigDecimal.valueOf(card.getPrice()));
        record.setRemark(card.getRemark());
        record.setDataStatus(QuanMaoCardDataStatus.NORMAL.real());
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    public void update(QuanMaoCardVoucher card, QuanMaoCard record, String updateUser) {
        record.setCardName(card.getCardName());
        record.setCardType(card.getCardType());
        record.setType(card.getType());
        record.setStatus(card.getStatus());
        record.setFaceValue(BigDecimal.valueOf(card.getFaceValue()));
        record.setPrice(BigDecimal.valueOf(card.getPrice()));
        record.setRemark(card.getRemark());
        record.setDataStatus(QuanMaoCardDataStatus.NORMAL.real());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.updateByPrimaryKeySelective(record, Column.excludes(Column.cardNo,
                Column.deleted,
                Column.createUser,
                Column.createTime));
    }

    public void setNotFoundSyncData(QuanMaoCard record, String updateUser) {
        record.setDataStatus(QuanMaoCardDataStatus.NORMAL.real());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.updateByPrimaryKeySelective(record,
                Column.dataStatus,
                Column.updateUser,
                Column.updateTime);
    }
}